package com.example.broadcastreceiver;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {

    private TextView tvInfo;
    private Button btnRegist,btnUnRegist,btnSendNormal,sendOrder;
    private MyListener listener;
    private MyAnotherReceiver receiver;
    private EditText edtInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       tvInfo = findViewById(R.id.tv_info);

       findView();

       setListener();






    }

    private void setListener() {
        listener = new MyListener();
        btnSendNormal.setOnClickListener(listener);
        btnUnRegist.setOnClickListener(listener);
        btnRegist.setOnClickListener(listener);
        sendOrder.setOnClickListener(listener);
    }

    private void findView() {
        btnRegist = findViewById(R.id.btn_regist);
        btnUnRegist = findViewById(R.id.btn_unregist);
        btnSendNormal = findViewById(R.id.btn_send_normal);
        edtInfo = findViewById(R.id.info);
        sendOrder = findViewById(R.id.send_order);
    }



    class MyListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btn_regist:
                    registerMyAnoterReceiver();
                    break;
                case R.id.btn_unregist:
                    //取消自定义广播接收器
                    upRegistMyAnotherReceiver();
                    break;
                case R.id.btn_send_normal:
                    //发布普通股广告
                    sendNormalBoardcast();
                    break;
                case R.id.send_order:
                    //发送有序广播
                    sendOrderBroadcast();
                    break;
            }
        }
    }

    private void sendOrderBroadcast() {
        //按照优先级次序依次往后传
        //创建intent对象，并指定action
        Intent intent = new Intent("example.custom.receiver");
        //明确指定包名
        intent.setPackage(this.getPackageName());
        //intent对象封装参数
        intent.putExtra("info",edtInfo.getText().toString());
        //发送有序广播
        sendOrderedBroadcast(intent,null);

    }

    private void sendNormalBoardcast() {
        //创建intent对象，并指定action
        Intent intent = new Intent("example.custom.receiver");
        //必须明确指定包名
        intent.setPackage(this.getPackageName());
        //封装必要的参数
        intent.putExtra("info",edtInfo.getText().toString());
        //发送普通广播
        sendBroadcast(intent);
    }

    private void upRegistMyAnotherReceiver() {
        this.unregisterReceiver(receiver);
        Toast.makeText(this,
                "取消注册广播"
        ,Toast.LENGTH_LONG).show();
    }

    private void registerMyAnoterReceiver() {
        //创建用于封装Action的intentFilter对象
        IntentFilter filter = new IntentFilter("example.custom.receiver");
        //设置优先级
        filter.setPriority(1);
        //创建receiver对象
        receiver = new MyAnotherReceiver(tvInfo);
        //动态注册receiver，同时将receiver对象与Action关联
        //从而决定了receiver能够接收的广播类型
        this.registerReceiver(receiver,filter);
        Toast.makeText(this,
                "注册广播"
                ,Toast.LENGTH_LONG).show();



    }

}









